package com.twu.biblioteca;

import java.util.ArrayList;

public class BibliotecaApp {

    private static ArrayList<Book> bookShelf;

    public static ArrayList<Book> getBookShelf() {
        return bookShelf;
    }

    public static void setBookShelf() {
        ArrayList<Book> bookShelf = new ArrayList<Book>();

        bookShelf.add(new Book("Great Expectations", "Charles Dickens", "available"));
        bookShelf.add(new Book("The Adventures of Sherlock Holmes", "Arthur Conan Doyle", "reserved"));
        bookShelf.add(new Book("The Tipping Point: How Little Things Can Make a Big Difference", "Malcolm Gladwell", "available"));

        BibliotecaApp.bookShelf = bookShelf;
    }

    public static ArrayList<String> createMenu() {
        ArrayList<String> menu = new ArrayList<String>();
        menu.add(" List all books");
        menu.add(" Reserve a book");
        menu.add(" Membership");
        menu.add(" Exit");
        return menu;
    }

    public static int askForValidOptionFromMenu(ArrayList<String> optionList) {
        int input = StringHandler.returnValidInput(optionList.size());
        return input;
    }

    public static Book selectAValidBookFromBooks(ArrayList<Book> optionList) {
        String message = "\nPlease select a book for reservation\n";
        StringHandler.printlnMessage(message);
        int input = StringHandler.returnValidInput(optionList.size());
        return bookShelf.get(input - 1);
    }

    public static void printBookShelf() {
        Book.printTableHeading();
        for (Book book : BibliotecaApp.getBookShelf())
            book.print(book);
    }


    public static void selectFromMainMenu(int input) {
        switch (input) {
            case 1:
                printBookShelf();
                break;
            case 2:
                printBookShelf();
                Book selectedBook = selectAValidBookFromBooks(getBookShelf());
                Book.reserve(selectedBook);

                break;
            case 3:
                StringHandler.printlnMessage("\nPlease talk to a Librarian. Thank you.\n");
                break;
            case 4: {
                StringHandler.printlnMessage("\nThank you for using Biblioteca\n");
                System.exit(0);
                break;
            }
        }
    }


    public static void start() {
        String welcomeMessage = "\nWelcome to Biblioteca!";
        StringHandler.printlnMessage(welcomeMessage);
        BibliotecaApp.setBookShelf();
        StringHandler.printMenu(BibliotecaApp.createMenu());
    }

    public static void main(String[] args) {
        start();
        while (true) {
            selectFromMainMenu(askForValidOptionFromMenu(createMenu()));
            StringHandler.printMenu(BibliotecaApp.createMenu());
        }

    }

}
